-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[inline-asm] Fixed issue no. 164973 #167316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…pp and adding a clang CodeGen test named "asm-srcloc-split-literal.c".
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang Author: Anakala Sashikiran (AnSaki57) ChangesMade modifications to CGStmt.cpp and added a clang CodeGen test named "asm-srcloc-split-literal.c". Full diff: https://github.com/llvm/llvm-project/pull/167316.diff 2 Files Affected:
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 36be3295950b8..5a062b74b0607 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2572,24 +2572,49 @@ CodeGenFunction::EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
CodeGenFunction &CGF) {
SmallVector<llvm::Metadata *, 8> Locs;
+
+ // We need these to find the correct location for the first line.
+ StringRef StrVal = Str->getString();
+ const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
+ const LangOptions &LangOpts = CGF.CGM.getLangOpts();
+ unsigned StartToken = 0;
+ unsigned ByteOffset = 0;
+
// Add the location of the first line to the MDNode.
+
+ // Find the offset of the first character that isn't horizontal whitespace.
+ size_t FirstLocOffset = StrVal.find_first_not_of(" \t\v\f");
+
+ // If the string is empty or all-whitespace, default to offset 0.
+ if (FirstLocOffset == StringRef::npos)
+ FirstLocOffset = 0;
+
+ SourceLocation FirstLineLoc = Str->getLocationOfByte(
+ FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+
Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
- CGF.Int64Ty, Str->getBeginLoc().getRawEncoding())));
- StringRef StrVal = Str->getString();
- if (!StrVal.empty()) {
- const SourceManager &SM = CGF.CGM.getContext().getSourceManager();
- const LangOptions &LangOpts = CGF.CGM.getLangOpts();
- unsigned StartToken = 0;
- unsigned ByteOffset = 0;
+ CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
+ if (!StrVal.empty()) {
// Add the location of the start of each subsequent line of the asm to the
// MDNode.
for (unsigned i = 0, e = StrVal.size() - 1; i != e; ++i) {
if (StrVal[i] != '\n') continue;
+
+ // The next line starts at byte offset i + 1.
+ // Find the first non-horizontal-whitespace at or after this offset.
+ size_t NextLineOffset = StrVal.find_first_not_of(" \t\v\f", i + 1);
+
+ // If the rest of the string is empty or all-whitespace,
+ // just use the location right after the newline (i + 1).
+ if (NextLineOffset == StringRef::npos)
+ NextLineOffset = i + 1;
+
SourceLocation LineLoc = Str->getLocationOfByte(
- i + 1, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+ NextLineOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+
Locs.push_back(llvm::ConstantAsMetadata::get(
- llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
+ llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
}
}
diff --git a/clang/test/CodeGen/asm-srcloc-split-literal.c b/clang/test/CodeGen/asm-srcloc-split-literal.c
new file mode 100644
index 0000000000000..a1d898264e6bd
--- /dev/null
+++ b/clang/test/CodeGen/asm-srcloc-split-literal.c
@@ -0,0 +1,23 @@
+/// Test that inline asm source location corresponds to the actual
+/// instruction line, not the first line of the asm block.
+///
+/// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s
+
+// #include <stdint.h>
+// #include <string.h>
+
+void *memset(void *dest, int c, int n)__attribute__((naked));
+void *memset(void *dest, int c, int n) {
+ __asm__(
+ "\t" // <-- line with only a tab
+ "xchg %eax, %eax\n" // <-- A valid instruction
+ "\t" // <-- line with only a tab
+ "mov rdi, 1\n" // <-- An invalid instruction
+ );
+}
+
+int main() { return 0; }
+
+// CHECK: error: unknown use of instruction mnemonic
+// CHECK-NEXT: mov rdi, 1
+
|
You can test this locally with the following command:git-clang-format --diff origin/main HEAD --extensions c,cpp -- clang/test/CodeGen/asm-srcloc-split-literal.c clang/lib/CodeGen/CGStmt.cpp --diff_from_common_commit
View the diff from clang-format here.diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 5a062b74b..94a5c338e 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -2590,10 +2590,10 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
FirstLocOffset = 0;
SourceLocation FirstLineLoc = Str->getLocationOfByte(
- FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+ FirstLocOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
- Locs.push_back(llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
- CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
+ Locs.push_back(llvm::ConstantAsMetadata::get(
+ llvm::ConstantInt::get(CGF.Int64Ty, FirstLineLoc.getRawEncoding())));
if (!StrVal.empty()) {
// Add the location of the start of each subsequent line of the asm to the
@@ -2610,11 +2610,12 @@ static llvm::MDNode *getAsmSrcLocInfo(const StringLiteral *Str,
if (NextLineOffset == StringRef::npos)
NextLineOffset = i + 1;
- SourceLocation LineLoc = Str->getLocationOfByte(
- NextLineOffset, SM, LangOpts, CGF.getTarget(), &StartToken, &ByteOffset);
+ SourceLocation LineLoc =
+ Str->getLocationOfByte(NextLineOffset, SM, LangOpts, CGF.getTarget(),
+ &StartToken, &ByteOffset);
Locs.push_back(llvm::ConstantAsMetadata::get(
- llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
+ llvm::ConstantInt::get(CGF.Int64Ty, LineLoc.getRawEncoding())));
}
}
|
efriedma-quic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the title to explain what you're fixing. It's good to put something like "Fixes #164973" somewhere, but not the title/first line.
| int main() { return 0; } | ||
|
|
||
| // CHECK: error: unknown use of instruction mnemonic | ||
| // CHECK-NEXT: mov rdi, 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CHECK lines here don't seem to actually check the line numbers.
| /// Test that inline asm source location corresponds to the actual | ||
| /// instruction line, not the first line of the asm block. | ||
| /// | ||
| /// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add -o %t or something like that so we don't accidentally get output files in weird places.
Needs // REQUIRES: x86-registered-target somewhere because this uses the x86 asm parser.
| /// RUN: not %clang_cc1 -triple x86_64-pc-linux-gnu -emit-obj %s 2>&1 | FileCheck %s | ||
|
|
||
| // #include <stdint.h> | ||
| // #include <string.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary comments.
|
|
||
| // The next line starts at byte offset i + 1. | ||
| // Find the first non-horizontal-whitespace at or after this offset. | ||
| size_t NextLineOffset = StrVal.find_first_not_of(" \t\v\f", i + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you rearrange the loop so you don't have to duplicate the code for the first line?
Made modifications to CGStmt.cpp and added a clang CodeGen test named "asm-srcloc-split-literal.c".